import { ETaskState } from "GameLoop/DecisionMaking/TaskDeclare/type";
import { ATaskAdapter } from ".";

import { FactoryTaskHelper } from "GameLoop/WorldState/EntityManager/Entitys/room/factory/FactoryTaskHelper";
import { PowerCreepArgHelper } from "utils/helper/Z_PowerCreepArgHelper";
import { FactoryState } from "GameLoop/WorldState/EntityManager/Entitys/room/factory/constants";

export class FactoryTaskAdapter extends ATaskAdapter<"factory">
{
    // constructor(context:IContextGetter,world:WorldGetter)
    // {
    //     super(context,world);
       
    // }
 
    public bindTasks(room: Room, tasks:ITaskDeclare["factory"][],customCheck:IConditionFunction): void 
    {
        for(const task of tasks)
        {
            if(customCheck(room as Required<Room>,task) == OK )
            {
                room.deleteTaskById(task.id,"factory");
                
            }
            else if(ETaskState.Complete == this.checkState(room,task))
            {
                //global.Log.Info(`${room.name} room ${task.id} check 已经完成`);
                room.deleteTaskById(task.id,"factory");
                
            }
           // global.Log.Info(`${room.name} room ${task.id} check 未完成`);
           break;
        }
    }

    protected checkState(room:Room,task:ITaskDeclare["factory"]):ETaskState
    {
        const coreentity = room.getEntity("CoreEntity");
        if(  !task.data.arg.isInit)
        {
            // FactoryTaskHelper.CreateProductStack(toptask.arg);
            task.data.arg.isInit = true;
        }
        // room.log("info","工厂任务检测")
        if(task.data.arg.transportKey && !room.findTaskById(task.data.arg.transportKey,"core"))
        {
            room.log("info","工厂任务检测","运输任务过期")
            task.data.arg.transportKey =undefined;
            room.log("info","清空运输任务后",task.data.arg.transportKey)
        }
        const toptask = task.data.arg.TopTask;
        if(!task.data.arg.TopTask.amount)
        {
            room.log("error",`此生产任务错误! ${JSON.stringify(toptask)}`);
            return ETaskState.Complete;
        }
        if(!coreentity.Factory)
        {
            return ETaskState.Complete;
        }
        
        if(COMMODITIES[ toptask.target].level && COMMODITIES[toptask.target].level != coreentity.Factory?.level)
        {
            // 等级不相等
         
            room.log("error",`此工厂等级无法生产此任务的商品! ${JSON.stringify(toptask)}`);
            return ETaskState.Complete;
        }
        if( task.data.arg.state == FactoryState.complete && !task.data.arg.transportKey)
        {
            return ETaskState.Complete;
        }

        const top_produce = FactoryTaskHelper.Top( task.data.arg);

        if(top_produce  )
        {
            if(COMMODITIES[top_produce.target].level && coreentity.Factory.cooldown == 0)
            {
                const opt_Factorytask = PowerCreepArgHelper.getOpRoomObjectTask(coreentity.Factory,PWR_OPERATE_FACTORY,room.name);
                if( opt_Factorytask && coreentity.isProduceReady(top_produce.target))
                {
                    // coreentity.raiseEvent(opt_Factorytask);
                    this.notify<"PowerEvent"> (room.name,{
                        region:room.name,
                        room:room.name,
                        POWER:PWR_OPERATE_FACTORY,
                        ObjectID:coreentity.Factory.id,
                        type:"PowerEvent"
                    })
                    room.log("warn",`OP 工厂进行生产!`)
                }
            }
        }
        else 
        {
            room.log("error",`没有生产列表为空! ${JSON.stringify(toptask)}`);
        }
        return  ETaskState.Running;
    }
    
}